//
//  SetupViewController.m
//  FreeTravel
//
//  Created by 徐康 on 16/8/30.
//  Copyright © 2016年 KX. All rights reserved.
//
#import "PhoneLoginViewController.h"
#import "SetupViewController.h"
#import "FreeTravelMacro.h"
@interface SetupViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *arrOfImage;
@property (nonatomic, strong) NSArray *arrOfName;
@property(nonatomic,retain)UIAlertController *alertView;
@property(nonatomic,assign)BOOL isDay;
@property(nonatomic,assign)float size_m;
@end
@implementation SetupViewController


- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    
    
}

- (void)goBack {
    [self.navigationController popViewControllerAnimated:NO];
    
}

- (void)viewDidLoad{
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = YES;
    
    
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"sdfasadfas"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.title = @"设置";
    [self handleData];
    [self createTableView];
}
- (void)handleData{
    self.arrOfName = @[@"账号",@"推送设置",@"清楚缓存"];
    self.arrOfImage = @[@"zhanghao",@"tuisong",@"huacun"];
}
- (void)createTableView{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SetupViewControllerCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"SetupViewControllerCell"];
    }
    if (indexPath.row == 0) {
        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"status"]) {
            cell.detailTextLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
        }else{
            cell.detailTextLabel.text = @"登录";
        }
        
    }
    
    cell.imageView.image = [UIImage imageNamed:self.arrOfImage[indexPath.row]];
    cell.textLabel.text = self.arrOfName[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row ==0) {
        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"status"]) {
            
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"确定登出账号?" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *certain = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"username"];
                [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"status"];
                [self.tableView reloadData];
            }];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alertVC addAction:certain];
            [alertVC addAction:cancel];
            [self presentViewController:alertVC animated:YES completion:^{
                
            }];
            
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"status"];
        }else{
            
            PhoneLoginViewController *vc = [[PhoneLoginViewController alloc]init];
            UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:vc];
            [self presentViewController:navi animated:YES completion:^{
                
            }];
        }
    }else if (indexPath.row == 1){
        
    }else{
        [self clearCache];
    }
}
//清除缓存
-(void)clearCache{
    //获取缓存大小
    [self getCacheSize];
    
    NSString *str = [NSString stringWithFormat:@"是否清除缓存 %fM",self.size_m];
    self.alertView = [UIAlertController alertControllerWithTitle:@"缓存清除" message:str preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    UIAlertAction *certain = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSString *cacheFilePath = [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"];
        
        NSFileManager *manager = [NSFileManager defaultManager];
        
        [manager removeItemAtPath:cacheFilePath error:nil];
        
        
    }];
    
    
    
    [self.alertView addAction:cancel];
    
    [self.alertView addAction:certain];
    
    [self presentViewController:self.alertView animated:YES completion:^{
        
        //NSLog(@"%@",[self getCacheSize]);
    }];
    
}
#pragma mark 缓存大小
-(NSString *)getCacheSize{
    //    //获取沙盒路径
    //    NSArray *sandBoxPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //
    //01.获取当前图片缓存路径
    NSString * cacheFilePath = [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"];
    
    //02.创建文件管理对象
    NSFileManager *filemanager = [NSFileManager defaultManager];
    
    //获取当前缓存路径下的所有子路径
    NSArray *subPaths = [filemanager subpathsOfDirectoryAtPath:cacheFilePath error:nil];
    
    long long sumSize = 0;
    
    
    //遍历所有子文件
    for (NSString *subPath in subPaths) {
        //1）.拼接完整路径
        NSString *filePath = [cacheFilePath stringByAppendingFormat:@"/%@",subPath];
        //2）.计算文件的大小
        long long fileSize = [[filemanager attributesOfItemAtPath:filePath error:nil]fileSize];
        //3）.加载到文件的大小
        sumSize += fileSize;
    }
    
    
    self.size_m = sumSize/(1024.0*1024.0);
    
    return [NSString stringWithFormat:@"%.2fM",self.size_m];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
